<?php

namespace Lesson\Controller;

use Common\Controller\AdminbaseController;

class AdminvideoController extends AdminbaseController {

    private $VIDEO_TYPE_VIDEO   = 1; // 视频
    private $VIDEO_TYPE_AUDIO   = 2; // 音频

    private $video_local_path   = './data/upload/video/';

    public function video_lists()
    {
        // 名字模糊搜索
        $keyword = I('request.keyword');
        if(!empty($keyword)) {
            $where['video_name'] = array('like', "%$keyword%");
        }
        $model  = M('video');
        $count  = $model->count();
        $page   = $this->page($count, $this->page_size);
        $lists  = $model->order('id desc')->select();

        foreach ($lists as $k => $v) {
            $v['size'] = sprintf("%.2f", $v['video_size'] / 1024 / 1024) . 'M';
            $lists[$k] = $v;
        }

        $this->assign("page", $page->show('Admin'));
        $this->assign("formget", array_merge($_GET, $_POST));
        $this->assign("lists", $lists);
        $this->display();
    }

    // 上传视频
    public function upload_video()
    {
        $upload_setting = sp_get_upload_setting();
        if (IS_POST) {
            $extensions     = explode(',', $upload_setting['video']['extensions']); // 文件类型
            $file_extension = sp_get_file_extension($_FILES['video']['name']);
            if (in_array($file_extension, $extensions)) { // 文件类型检测
                $upload_max_filesize = $upload_setting['upload_max_filesize'][$file_extension];
                if ($_FILES['video']['size'] <= $upload_max_filesize) { // 文件大小检测
                    $dest_name = $this->video_local_path . $_FILES['video']['name'];
                    if (!move_uploaded_file($_FILES['video']['tmp_name'], $dest_name)){ // 进行文件移动
                        $this->error("文件上传失败");
                    } else {

                        $has = M('video')->where(['video_name' => $_FILES['video']['name']])->find();
                        if ($has) {
                            $this->error('文件重复上传！');
                        } else {
                            // 上传阿里云
                            $videoId = upload_video_aliyun($dest_name, $_FILES['video']['name']);
                            M('video')->add([
                                'video_id'      => $videoId,
                                'video_name'    => $_FILES['video']['name'],
                                'video_size'    => $_FILES['video']['size'],
                                'created_at'    => time(),
                            ]);
                            // 把本地文件删除
                            unlink($dest_name);

                            $this->success('文件上传成功!', U('Lesson/Adminvideo/video_lists'));
                        }
                    }
                } else {
                    $this->error('文件大小超出');
                }
            } else {
                $this->error('文件类型错误');
            }
        } else {
           $this->display();
        }
    }

    // 根据线上阿里云的视频id，将信息录入
    public function upload_online_video()
    {
        if (IS_POST) {
            $video_id   = I('video_id');
            $video_name = I('video_name');
            if ($video_id && $video_name) {
                $has_video = M('video')->where("video_id = '" . $video_id . "' or video_name = '" . $video_name . "'")->find();
                if ($has_video) {
                    $this->error('该视频已添加，或者名称重复');
                } else {
                    $playInfo = get_video_play_info($video_id);
                    if ($playInfo) {
                        if (is_string($playInfo)) {
                            $this->error($playInfo);
                        } else {
                            $data = array(
                                'video_id'      => $video_id,
                                'video_name'    => $video_name,
                                'video_size'    => $playInfo[0]->Size,
                                'video_type'    => $playInfo[0]->StreamType == 'video' ? $this->VIDEO_TYPE_VIDEO : $this->VIDEO_TYPE_AUDIO,
                                'created_at'    => strtotime($playInfo[0]->CreationTime),
                            );
                            M('video')->add($data);
                            $this->success('添加成功');
                        }
                    } else {
                        $this->error('未获取到视频信息');
                    }
                }
            } else {
                $this->error('参数错误');
            }
        }
    }

    // 根据id获取信息
    public function get_video_info()
    {
        $video_id = I('video_id');
        if ($video_id) {
            $playInfo = get_video_play_info($video_id);
            if ($playInfo) {
                if (is_string($playInfo)) {
                    $this->error($playInfo);
                } else {
                    $data = array(
                        'status'        => 1,
                        'video_size'    => round(floatval($playInfo[0]->Size / 1024 / 1024),2),
                        'created_at'    => date('Y-m-d H:i', strtotime($playInfo[0]->CreationTime)),
                    );
                    $this->ajaxReturn($data);exit;
                }
            } else {
                $this->error('未获取到视频信息');
            }
        }
    }

    // 删除视频
    public function del_video()
    {
        $id = I('get.id');
        if ($id) {
            $video_info = M('video')->where(['id' => $id])->find();
            if ($video_info) {
                require_once './plugins/AliyunVodUpload/Autoloader.php';
                $myUtils    = new \MyUtils();
                $myUtils->del_video($video_info['video_id']);

                M('video')->where(['id' => $id])->delete();
                $this->success('删除成功');exit;
            }
        }
        $this->error('删除失败');
    }
}
